package aynu.hd.Admin.Controller;

import aynu.hd.Admin.Service.AdminRoomService;
import aynu.hd.pojo.Orders;
import aynu.hd.pojo.Room;
import aynu.hd.pojo.Schedule;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("admin/room")
public class AdminRoomController {
    @Autowired
    AdminRoomService adminRoomService;

    //管理员首页
    @RequestMapping("index")
    public String adminIndex(Model model) {
        //日期
        List dates = adminRoomService.queryDate();
        //金额
        List moneys = adminRoomService.queryMoneyDay(dates, model);
        //查看评论条目
        Integer msg = adminRoomService.queryCommentSize();
        //订单数量
        Integer orderSize = adminRoomService.queryOrdersSize();
        //房间数量
        Integer roomSize = adminRoomService.queryRoomSize();
        //用户数量
        Integer userSize = adminRoomService.queryUserSize();
        model.addAttribute("dates", dates);
        model.addAttribute("moneys", moneys);
        model.addAttribute("msg", msg);
        model.addAttribute("orderSize", orderSize);
        model.addAttribute("roomSize", roomSize);
        model.addAttribute("userSize", userSize);
        return "admin/index";
    }

    //房间评论消息状态设置
    @GetMapping("comment")
    public String comment(Model model) {
        //房间评论消息未处理查看
        List<Schedule> schedules = adminRoomService.queryRoomComment();
        //房间评论消息已经处理
        List<Schedule> scheduleList = adminRoomService.queryRoomComment1();
        model.addAttribute("schedules", schedules);
        model.addAttribute("scheduleList", scheduleList);
        model.addAttribute("msg", schedules.size());
        return "admin/roomComment";
    }

    //设置评论不发布
    @GetMapping("reduceScheduleComment")
    @ResponseBody
    public String reduceScheduleComment(String schedule_id, String state) {
        String data = adminRoomService.reduceScheduleComment(schedule_id, state);
        return data;
    }

    //订单页面
    @GetMapping("orderList")
    public String orderList(Model model) {
        //查看评论条目
        Integer msg = adminRoomService.queryCommentSize();
        model.addAttribute("msg", msg);
        //查询全部订单（未支付）
        List<Orders> orders = adminRoomService.orderListNo();
        model.addAttribute("orders", orders);
        //查询全部订单（已支付）
        List<Orders> ordersList = adminRoomService.orderListYes();
        model.addAttribute("ordersList", ordersList);
        return "admin/orderList";
    }

    //删除订单
    @GetMapping("removeOrders")
    @ResponseBody
    public String removeOrders(String orders_id) {
        String data = adminRoomService.removeOrders(orders_id);
        return data;
    }

    //人员信息页面
    @GetMapping("userList")
    public String userList(Model model) {
        //查看评论条目
        Integer msg = adminRoomService.queryCommentSize();
        model.addAttribute("msg", msg);
        List<User> users = adminRoomService.userList();
        model.addAttribute("users", users);
        return "admin/userList";
    }

    //新增房间页面
    @GetMapping("roomIncrease")
    public String roomIncrease(Model model) {
        //查看评论条目
        Integer msg = adminRoomService.queryCommentSize();
        model.addAttribute("msg", msg);
        return "admin/roomadd";
    }

    //进入房间列表页面
    @GetMapping("roomList")
    public String roomList(Model model, String add) {
        if (add != null) {
            model.addAttribute("add", add);
        }
        List<Room> roomList = adminRoomService.queryRoomAll();
        Integer msg = adminRoomService.queryCommentSize();
        model.addAttribute("msg", msg);
        model.addAttribute("roomList", roomList);
        return "admin/roomList";
    }

    //删除房间
    @GetMapping("roomDelete")
    @ResponseBody
    public String roomDelete(String room_id) {
        String data = adminRoomService.roomDelete(room_id);
        return data;
    }

    //新增房间
    @PostMapping("roomAdd")
    public String roomAdd(Room room, MultipartFile file1, MultipartFile file2, MultipartFile file3) {
        try {
            if (!file1.isEmpty()) {
                String imgUrl = saveFile(file1);
                room.setRoom_path1(imgUrl);
            }
            if (!file2.isEmpty()) {
                String imgUrl = saveFile(file2);
                room.setRoom_path2(imgUrl);
            }
            if (!file2.isEmpty()) {
                String imgUrl = saveFile(file3);
                room.setRoom_path3(imgUrl);
            }
            int rows = adminRoomService.roomAdd(room);
            if (rows > 0) {
                return "redirect:/admin/room/roomList?add=add";
            }
        } catch (RuntimeException | IOException e) {
            System.out.println(e.getCause());
        }
        return "hotel/orders/error";
    }

    //修改房间信息
    @GetMapping("roomModifyPage")
    public String roomModifyPage(String room_id, Model model) {
        Room room = adminRoomService.roomModifyPage(room_id);
        model.addAttribute("room", room);
        Integer msg = adminRoomService.queryCommentSize();
        model.addAttribute("msg", msg);
        return "admin/roomupdate";
    }

    //修改房间信息
    @PostMapping("roomModify")
    public String roomModify(Room room, MultipartFile file1, MultipartFile file2, MultipartFile file3) {
        Room room1 = adminRoomService.roomModifyPage(room.getRoom_id().toString());
        try {
            if (!file1.isEmpty()) {
                String imgUrl = saveFile(file1);
                room.setRoom_path1(imgUrl);
            }else {
                room.setRoom_path1(room1.getRoom_path1());
            }
            if (!file2.isEmpty()) {
                String imgUrl = saveFile(file2);
                room.setRoom_path2(imgUrl);
            }else {
                room.setRoom_path2(room1.getRoom_path2());
            }
            if (!file2.isEmpty()) {
                String imgUrl = saveFile(file3);
                room.setRoom_path3(imgUrl);
            }else {
                room.setRoom_path3(room1.getRoom_path3());
            }
            int rows = adminRoomService.roomModify(room);
            if (rows > 0) {
                return "redirect:/admin/room/roomList?add=modify";
            }
        } catch (RuntimeException | IOException e) {
            System.out.println(e.getCause());
        }
        return "hotel/orders/error";
    }

    String saveFile(MultipartFile file) throws IOException {
        String path = ResourceUtils.getURL("classpath:").getPath();
        System.out.println("--------------------" + path + "----------------");
        File filePath = new File(path + "/static/img/room");
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        String originalFileName = file.getOriginalFilename();
        StringBuffer fileName = new StringBuffer(UUID.randomUUID().toString().replace("-", ""));
        fileName.append("_").append(originalFileName);
        File finalFile = new File(filePath, fileName.toString());
        file.transferTo(finalFile);
        return "/img/room/" + fileName.toString();
    }
}
